﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Sudoku.Menu
{
	public enum LabelStyle
	{
		CENTER,
		TOP,
		BOTTOM,
		RIGHT,
		LEFT,
	}

	public class LabelButton : Button
	{

		protected SpriteFont font;
		protected String label;
		protected Color labelColor;
		protected LabelStyle labelStyle;
		private Vector2 labelPos;

		public LabelButton(Game game, Rectangle rect, String label, SpriteFont font, Color labelColor, LabelStyle style)
			: base(game, rect)
		{
			this.font = font;
			this.label = label;
			this.labelColor = labelColor;
			labelStyle = style;
			CalculateLabelPosition();
		}

		public LabelButton(Game game, Rectangle rect, String label, SpriteFont font, Color labelColor)
			: this(game, rect, label, font, labelColor, LabelStyle.CENTER)
		{
			this.font = font;
			this.label = label;
			this.labelColor = labelColor;
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);

			// Draw label
			spriteBatch.Begin();

			labelColor.A = this.Enabled ? (byte)255 : (byte)150;

			spriteBatch.DrawString(font, label, labelPos, labelColor);

			spriteBatch.End();
		}

		public String Label
		{
			get { return label; }
			set { label = value; CalculateLabelPosition(); }
		}

		public LabelStyle Style
		{
			get { return labelStyle; }
			set { labelStyle = value; CalculateLabelPosition(); }
		}

		private void CalculateLabelPosition()
		{
			Vector2 size = font.MeasureString(label);
			Vector2 diff = new Vector2(rect.Width, rect.Height) - size;

			switch(labelStyle)
			{
				case LabelStyle.CENTER:
					labelPos.X = rect.X + diff.X * 0.5f;
					labelPos.Y = rect.Y + diff.Y * 0.5f;
					break;

				case LabelStyle.TOP:
					labelPos.X = rect.X + diff.X*0.5f;
					labelPos.Y = rect.Y + diff.Y * 0.1f;
					break;

				case LabelStyle.BOTTOM:
					labelPos.X = rect.X + diff.X * 0.5f;
					labelPos.Y = rect.Y + diff.Y * 0.9f;
					break;

				default: 
					labelPos.X = rect.X + diff.X * 0.5f;
					labelPos.Y = rect.Y + diff.Y * 0.5f;
					break;
			}
			
			
		}

	}
}
